package cn.yanhu.kuwanapp.home.fragment

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseFragment
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.databinding.FragmentInviteBinding
import cn.yanhu.kuwanapp.databinding.FragmentWelfareBinding
import cn.yanhu.kuwanapp.event.VideoFinishEvent
import cn.yanhu.kuwanapp.home.MainViewModel
import cn.yanhu.kuwanapp.invite.InviteFragment
import cn.yanhu.kuwanapp.utils.helper.WebViewHelper
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 *
 * 类描述：福利
 * @author：woochen
 * create at：2021/7/27 10:38 PM
 */
class WelfareFragment:BaseFragment<FragmentWelfareBinding,MainViewModel>() {

    override fun setContentView(): Int = R.layout.fragment_welfare

    override fun initData() {
        showLoading(true)
        WebViewHelper.configSetting(mContext,mBinding.webView, {
            showContent()
        })
        requestData()
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //视频观看结束
        LiveEventBus.get(VideoFinishEvent::class.java).observe(this, Observer {
            mBinding.webView.reload()
        })
    }

    override fun requestData() {
        WebViewHelper.loadUrl(mBinding.webView,mUrl)
    }

    fun canBack(): Boolean {
        if (mBinding.webView.canGoBack()) {
            mBinding.webView.goBack()
            return true
        } else return false
    }

    private val mUrl by lazy {
        arguments?.getString(EXTRA_URL)?:""
    }

    fun refreshData() {
        requestData()
    }
    companion object{
        private const val EXTRA_URL = "extra_url"
        fun newInstance(url: String) = WelfareFragment().apply {
            arguments = Bundle().apply {
                putString(EXTRA_URL, url)
            }
        }
    }
}